const Service = require('egg').Service;
var fs       = require('fs');
const common = require('../common/common')

const puppeteer = require('puppeteer');

class TianMaoService extends Service {

  async openBower(url){
    let browser = null;
    let page = null;
    let text = null;
    if(!global.bowerauto || !global.bowerauto.browser){
        global.bowerauto = {};
        browser = await puppeteer.launch({
            headless: false,
            ignoreDefaultArgs: ["--enable-automation"]
          } );

        page = await browser.newPage();

        global.bowerauto.browser = browser;
        global.bowerauto.page = page;

        console.log("initing");
    }else{
        browser = global.bowerauto.browser;
        page = global.bowerauto.page;
    }
    try{
      await page.goto(url);
    }catch(e){
      console.log(e);
      global.bowerauto = {};
      browser = await puppeteer.launch({
          headless: false,
          ignoreDefaultArgs: ["--enable-automation"]
        } );

      page = await browser.newPage();

      global.bowerauto.browser = browser;
      global.bowerauto.page = page;

      await page.goto(url);
    }
  }

    async doService(params) {
      if(params.operType == "openBower"){
        this.openBower('https://login.tmall.com');
      }else if(params.operType == "bind"){
        try{
          await global.bowerauto.page.goto("https://chaoshi.tmall.com/gift-card.htm");
        }catch(e){
          console.log("错误"+e);
        }
        let pages = await global.bowerauto.browser.pages();
        global.bowerauto.page = pages[pages.length-1];
        await common.timeout(4000);
        await global.bowerauto.page
    .mainFrame()
    .addScriptTag({
      url: 'https://cdn.bootcss.com/jquery/3.2.0/jquery.min.js'
    });
    await common.timeout(4000);
        global.bowerauto.page.click('.account-info .bind-card-btn');  
        
        await common.timeout(4000);
        await global.bowerauto.page.type('#input-card-num','1231231231232313333');
        await global.bowerauto.page.type('#input-card-pwd','123123123123123');
        global.bowerauto.page.click('.btn.btn-ok');
        await common.timeout(3000);
         let element = await global.bowerauto.page.$(".done-msg");
        let  text = await global.bowerauto.page.evaluate(element => element.textContent, element);

        global.bowerauto.page.click('.btn.btn-ok');
        return {code:"00" ,msg: text};
      }
       
        // await page.type('#main-phone',params.phone);
        // page.click('.btn.btn-ok');
        // await common.timeout(4000);
      
        // let isSuccess = true;
        // let element = await page.$("#receive-desc");
        // if(element == null){
        //   isSuccess = false;
        //   element = await page.$("#error-title");
        // }
        // text = await page.evaluate(element => element.textContent, element);
        // if(isSuccess && params.phone==text.split(" ")[1]){
        //   return {code:"00" ,msg: "充值成功"};
        // }else{
        //   return {code:"01" ,msg: "充值失败"};
        // }
        return {code:"00" ,msg: "充值成功"};
    }
  }
  
  module.exports = TianMaoService;